package com.example.lucene.controller;

import com.example.lucene.service.KnowledgeBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author yanggld
 * @Date 2019/5/24-16:27
 * @Description
 */
@RestController
public class IndexController {

	@Autowired
	private KnowledgeBaseService knowledgeBaseService;

	@PostMapping("/api/creatIndex")
	public void createKnowledgeBaseIndex(@RequestParam("product") String[] product,
	                                     @RequestParam(value = "mode", defaultValue = "create", required = false) String mode, HttpServletResponse response) {
		StringBuilder sbProducts = new StringBuilder();
		if (product == null || product.length == 0) {
			System.out.println("CreateIndex no product!");
			return;
		}
		for (String productItem : product) {
			sbProducts.append(productItem);
			sbProducts.append(" and ");
		}
		int length;
		//去除多餘的 and
		if ((length = sbProducts.length()) > 0) {
			sbProducts.delete(length - 5, length);
		}
		String products = sbProducts.toString();
		try {
			System.out.println("CreateIndex product: "+ products +" ~~~~~~~~~~ Start");
			knowledgeBaseService.createIndex(product, mode);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("CreateIndex product: "+ products +" ~~~~~~~~~~ Completed");
	}
}
